package util

import (
	"fmt"
	"net/http"
)

var (
	ErrRefreshTokenFailed = &Error{
		Code:    http.StatusInternalServerError,
		Message: "续约Token失败",
	}

	ErrInternalServer = NewError(http.StatusInternalServerError, "服务器发生错误")
)

type (
	Error struct {
		Code    int    `json:"code"`
		Message string `json:"message"`
	}
)

func (e Error) Error() string {
	return fmt.Sprintf("%d: %s", e.Code, e.Message)
}

func Unwrap(err error) *Error {
	if v, ok := err.(*Error); ok {
		return v
	}
	return nil
}

func NewError(code int, message string) error {
	return &Error{
		Code:    code,
		Message: message,
	}
}

func NewResourceNotFoundError(message string) error {
	return &Error{
		Code:    http.StatusNotFound,
		Message: message,
	}
}

func NewUnauthorizedRequestError(message string) error {
	return &Error{
		Code:    http.StatusUnauthorized,
		Message: message,
	}
}

func NewInvalidParameterError(message string) error {
	return &Error{
		Code:    1200,
		Message: message,
	}
}
